/*
 * @Description: 孵化 Store
 * @Author: xuyang
 * @Date: 2019-06-09 15:06:14 
 * @Last Modified by: XuYang
 * @Last Modified time: 2019-06-15 15:58:43
 */

import { observable, action, computed, runInAction } from 'mobx';
import {
    getInviteStatisticsDataApi,
    getInviteTreeDataApi,
    getPromoteIndexApi
} from '../api/invite';

export default class Login {
    @observable inviteStatistics = {
        depositsum: 0,
        accumulated: 0,
        yesterday_income:0,
        title:'邀请记录',
        hatch: false,
        icon: 'users',
        url:'Invite',
    }; //孵化统计 
    @observable ranks = []
    @observable inviteTree = [];
    @observable inviteIndex = {
        inviteCode: '',
        inviteUrl:'',
        inviteUrl64: '',
        androidUrl:'',
        iosUrl: ''
    };
    constructor(rootStore) {
        this.rootStore = rootStore;
    }
    @observable setRank (ranks){
        this.ranks = ranks;
    }
    @action setInviteStatistics (hatch){
        this.inviteStatistics.depositsum =  hatch.invot_sum;
        this.inviteStatistics.accumulated = hatch.accumulated;
        this.inviteStatistics.yesterday_income = hatch.yesterday_income;
    }
    @action getInviteStatistics (){
        return this.inviteStatistics;
    }
    @action setInviteTree (treeData){
        this.inviteTree = treeData;
    }
    @action getInviteTree (){
        return this.inviteTree;
    }
    @action setInviteIndex (inviteIndex){
        this.inviteIndex = {
            inviteCode: inviteIndex.invite_code,
            inviteUrl: inviteIndex.invite_url,
            inviteUrl64: inviteIndex.invite_img_base64.replace('\\',''),
            androidUrl: inviteIndex.apk_down_img_base64.replace('\\',''),
            iosUrl: inviteIndex.app_down_img_base64.replace('\\','')
        };
    }
    @action getInviteIndex (){
        return this.inviteIndex;
    }
    /**
     * 获取邀请统计
     */
    @action getInviteStatisticsData = async () => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getInviteStatisticsDataApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    } 
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setInviteStatistics(response.data);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 获取邀请树
     */
    @action getInviteTreeData = async () => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getInviteTreeDataApi(ret);
                    if(response.status === 11001){
                        //登录失效 重新登录
                        this.rootStore.loginStore.loginInvalid();
                        return;
                    } 
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setInviteTree(response.data);
                        });
                    } else if(response.status === 90001){
                        //数据为空
                        this.setInviteTree([]);
                    }else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
    /**
     * 获取邀请数据
     */
    @action getPromoteIndex = async (cb) => {
        try { 
            storage.load({ key: 'token' }).then(async (ret) => {
                if (ret) {
                    const response = await getPromoteIndexApi(ret);
                    if (response.status === 10000) {
                        runInAction(() => {
                            this.setInviteIndex(response.data);
                            this.setRank(response.data.ranks);
                        });
                    }else if(response.status === 90001){
                        //数据为空
                    } else {
                        this.rootStore.uiStore.setErrorMsg(response.message);
                    }
                }
              }).catch(() => {
                this.rootStore.uiStore.setErrorMsg(response.message);
              });
           
        } catch(e) {
            this.rootStore.uiStore.setErrorMsg(response.message);
        }
    }
}